﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OnlineMovieTicket.Data.IRepository;
using OnlineMovieTicket.Model;


namespace OnlineMovieTicket.Data.Repository
{
     

   public class TopTenMoviesRepository : ITopTenMoviesRepository
    {
        ONLINEMOVIETICKETEntities dataContent;


        public TopTenMoviesRepository()
        {
            dataContent = new ONLINEMOVIETICKETEntities();
        }

        public List<Model.TopTenMoviesDetail> GetTopTenMoviesByRating(DateTime StartDate, DateTime EndDate)
        {
            DateTime newStartDate;
            DateTime newEndDate ;
            if (StartDate != null)
            {
                newStartDate = DateTime.Parse(StartDate.ToString("dd-MMM-yyyy"));
            }
            else
            {
                newStartDate = DateTime.Today;

            }
            if( EndDate != null)
            {
            newEndDate = DateTime.Parse(EndDate.ToString("dd-MMM-yyyy"));
            }
            else
            {
                newEndDate = DateTime.Today;

            }
            List<Model.TopTenMoviesDetail> movieList = new List<Model.TopTenMoviesDetail>();
           
            var r = (from dr in dataContent.Reviews
                     join dm in dataContent.Movies
                     on dr.MovieCode equals dm.MovieCode
                     where  dm.ReleaseDate >= newStartDate && dm.ReleaseDate <= newEndDate
                     group dr by dr.MovieCode into d
                     select new
                     {
                         MovieCode = d.Key,
                         Avg_Rate = d.Average(f=> f.Rating)
    
                         
                     });
            r = r.OrderByDescending(a => a.Avg_Rate).Take(10);

            foreach (var item in r)
            {
                Model.TopTenMoviesDetail movDetail = new Model.TopTenMoviesDetail();
                Movies m = GetMovie(item.MovieCode);
                movDetail.topTenMoviesList = new List<Movies>();
                movDetail.topTenMoviesList.Add(m);
                movDetail.TotalRate = Math.Round( double.Parse(item.Avg_Rate.ToString()),2);
                movieList.Add(movDetail);

            }

            return movieList;
        }

                    
       
       
        public List<Model.TopTenMoviesDetail> GetTopTenMoviesByTicket(DateTime StartDate, DateTime EndDate)
        {
            DateTime newStartDate = DateTime.Parse(StartDate.ToString("dd-MMM-yyyy"));
            DateTime newEndDate = DateTime.Parse(EndDate.ToString("dd-MMM-yyyy"));
            List<Model.TopTenMoviesDetail> movieList = new List<Model.TopTenMoviesDetail>();

            var q = from t in dataContent.Transactions
                    join td in dataContent.TransactionDetails
                    on t.TransactionID equals td.TransactionID
                    join mtt in dataContent.MovieTimeTables
                    on t.MovieAssignmentID equals mtt.MovieAssignmentID
                    join m in dataContent.Movies
                    on mtt.MovieCode equals m.MovieCode
                    where m.ReleaseDate >= newStartDate && m.ReleaseDate <= newEndDate
                    && t.Status== "A"
                    group m by m.MovieCode
                    into g
                    orderby g.Count() descending
                    select new { MovieCode = g.Key, TicketCount = g.Count()};
            q = q.Take(10);
            
            foreach (var item in q)
            {
                Model.TopTenMoviesDetail movDetail = new Model.TopTenMoviesDetail();
                Movies m = GetMovie(item.MovieCode);
                movDetail.topTenMoviesList = new List<Movies>();
                movDetail.topTenMoviesList.Add(m);
                movDetail.TotalTicketSold = Int32.Parse(item.TicketCount.ToString());
                movieList.Add(movDetail);

            }

            return movieList;


        }


        public Movies GetMovie(int MovieCode)
        {
            var rmovie = from dt in dataContent.Movies
                    where dt.MovieCode == MovieCode
                    select new Movies
                    {
                        MovieCode = dt.MovieCode,
                        MovieName = dt.MovieName,
                        Synopsis = dt.Synopsis,
                        Language = dt.Language,
                        Duration = dt.Duration,
                        Status = dt.Status,
                        ReleaseDate = dt.ReleaseDate,
                    };

            return rmovie.First();
        }

        

      


    }
}
